package com.example.ceshi.ui.notifications;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.example.ceshi.Bean.NowUserData;
import com.example.ceshi.DetailActivity;
import com.example.ceshi.LoginActivity;
import com.example.ceshi.R;
import com.example.ceshi.SQLite.MySQLiteOpenHelper;
import com.example.ceshi.UserDetails;
import com.example.ceshi.databinding.FragmentNotificationsBinding;
import com.example.ceshi.utils.DestroyActivityUtil;
import com.example.ceshi.utils.ImageUtil;

import java.util.Objects;


//我的信息页面
public class NotificationsFragment extends Fragment {

    private FragmentNotificationsBinding binding;
    private String TAG = "ning";
    private TextView showName;
    private TextView username;
    private ImageView touxiang;
    private TextView title;
    private TextView nowdata;
    private TextView forupdate;
    private TextView noupdate;
    private MySQLiteOpenHelper mySQLiteOpenHelper;
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        NotificationsViewModel notificationsViewModel =
                new ViewModelProvider(this).get(NotificationsViewModel.class);

        binding = FragmentNotificationsBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

//        final TextView textView = binding.textNotifications;
//        notificationsViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);
        return root;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mySQLiteOpenHelper=new MySQLiteOpenHelper(getContext());




        view.findViewById(R.id.back_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(getActivity(), LoginActivity.class);

                startActivity(intent);

            }
        });
        view.findViewById(R.id.main).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), UserDetails.class);

                startActivity(intent);
            }
        });
        view.findViewById(R.id.update_password).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Dialog dialog = new Dialog(getContext());
                View inflate = getLayoutInflater().inflate(R.layout.forupdatedetail, null);
                dialog.setContentView(inflate);

                title = dialog.findViewById(R.id.update_title);
                nowdata = dialog.findViewById(R.id.nowdate);
                forupdate = dialog.findViewById(R.id.forupdate);
                noupdate = dialog.findViewById(R.id.noupdate);

                title.setText("修改密码");

                forupdate.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String data = String.valueOf(nowdata.getText()).trim();
                        if (data.equals("")) {
                            Toast.makeText(getContext(), "请输入内容", Toast.LENGTH_LONG).show();
                        } else {
                            dialog.dismiss();
                            NowUserData.password=data;

                            long id= mySQLiteOpenHelper.updateUserDate();
                            if(id!=-1){
                                Toast.makeText(getContext(), "修改成功", Toast.LENGTH_LONG).show();
                            }else{
                                Toast.makeText(getContext(), "修改失败", Toast.LENGTH_LONG).show();
                            }
                        }
                        //进行数据库操作


                    }
                });

                noupdate.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog.dismiss();
                    }
                });


                dialog.show();
            }
        });
        view.findViewById(R.id.update_showName).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Dialog dialog = new Dialog(getContext());
                View inflate = getLayoutInflater().inflate(R.layout.forupdatedetail, null);
                dialog.setContentView(inflate);

                title = dialog.findViewById(R.id.update_title);
                nowdata = dialog.findViewById(R.id.nowdate);
                forupdate = dialog.findViewById(R.id.forupdate);
                noupdate = dialog.findViewById(R.id.noupdate);

                title.setText("修改昵称");


                forupdate.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String data = nowdata.getText().toString();
                        if (data.equals("")) {
                            Toast.makeText(getContext(), "请输入内容", Toast.LENGTH_LONG).show();
                        } else {
                            NowUserData.showName = data;
                            showName.setText(data);
                            dialog.dismiss();
                            long id= mySQLiteOpenHelper.updateUserDate();
                            if(id!=-1){
                                Toast.makeText(getContext(), "修改成功", Toast.LENGTH_LONG).show();
                            }else{
                                Toast.makeText(getContext(), "修改失败", Toast.LENGTH_LONG).show();
                            }
                            //进行数据库操作
                        }


                    }
                });

                noupdate.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog.dismiss();
                    }
                });


                dialog.show();
            }
        });

        showName = view.findViewById(R.id.shouName);
        username = view.findViewById(R.id.username);
        touxiang = view.findViewById(R.id.touxiang);
        if(!Objects.equals(NowUserData.showName, "")&&NowUserData.showName!=null){
            showName.setText(NowUserData.showName);
        }

        username.setText("账号：" + NowUserData.username);
        if (NowUserData.imgPath.equals("")) {
            String TAG3="touxiang";
            Log.d(TAG3,"没有头像");
            touxiang.setImageResource(R.drawable.ic_touxiang);
        } else {
            String TAG3="touxiang";
            Log.d(TAG3,"有头像");
            touxiang.setImageBitmap(ImageUtil.base64ToImage(NowUserData.imgPath));
        }
    }


    @Override
    public void onResume() {
        super.onResume();

        if (NowUserData.imgPath.equals("")) {
            touxiang.setImageResource(R.drawable.ic_touxiang);
        } else {
            touxiang.setImageBitmap(ImageUtil.base64ToImage(NowUserData.imgPath));
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }


}